/**
 * 校园智能快递集散系统
 * 程序设计基础课程设计(2024级)
 *
 * 本文件包含用户管理模块的接口定义
 */

#ifndef USER_MANAGEMENT_C
#define USER_MANAGEMENT_C
#include "system_types.h"
#include "user_management.h"
 //--------------------------------
 // 其他函数
 //--------------------------------
 /**
    * 判断手机号码是否合法
    *
    * @param phone 号码
    * @return 如果号码合法返回 true，否则返回 false
    */
bool isPhoneNumber(const char* phone) {
    for (int i = 0; phone[i] != '\0'; i++) {
        if (!isdigit(phone[i])) {
            return false;
        }
    }

    return true;
}
/**
 * 判断密码是否合法
 *
 * @param password 密码
 * @return 如果密码合法返回 true，否则返回 false
 */
bool isPasswordValid(const char* password) {
    if (password == NULL || *password == '\0') {
        return false;
    }
    const char* allowedSpecialChars = "!@#$%^&*()_+-=[]{}|;:',.<>?";
    for (int i = 0; password[i] != '\0'; i++) {
        char c = password[i];
        if (!isdigit(c) && !isalpha(c) && strchr(allowedSpecialChars, c) == NULL) {
            return false;
        }
    }
    return true;
}
/**
 * 判断用户名是否合法
 *
 * @param username 用户名
 * @return 如果用户名合法返回 true，否则返回 false
 */
bool isUsernameValid(const char* username) {
    if (username == NULL || *username == '\0') {
        return false;
    }
    for (int i = 0; username[i] != '\0'; i++) {
        char c = username[i];
        if (!isdigit(c) && !isalpha(c)) {
            return false;
        }
    }
    return true;
}
//--------------------------------
// 用户管理函数
//--------------------------------

/**
 * 用户注册
 *
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param password 密码
 * @param phone 手机号
 * @param address 地址
 * @param type 用户类型
 * @param currentUser 当前操作用户(如果是管理员创建其他用户)，NULL表示自注册
 * @return 成功返回OK，失败返回错误码
 */
ErrorCode registerUser(SystemData* data, const char* username, const char* password,
    const char* phone, const char* address, UserType type, User* currentUser) {
    // 检查输入参数是否有效
    if (username == NULL || password == NULL || phone == NULL || address == NULL || !isPhoneNumber(phone) || !isUsernameValid(username) || !isPasswordValid(password)) {
        return ERR_INVALID_PARAM;
    }
    //检查手机号是否注册过
    User* current = data->userList;
    while (current != NULL) {
        if (strcmp(current->phone, phone) == 0) {
            return ERR_DUPLICATE;//重复
        }
        current = current->next;
    }

    if (!currentUser) {
        currentUser = (User*)malloc(sizeof(User));
    }
    // 创建新用户
    strncpy(currentUser->username, username, MAX_USERNAME_LEN);
    strncpy(currentUser->password, password, MAX_PASSWORD_LEN);
    strncpy(currentUser->phone, phone, MAX_PHONE_LEN);
    strncpy(currentUser->address, address, MAX_ADDRESS_LEN);
    currentUser->type = type;
    currentUser->permissions = data->rolePermissions[type];
    currentUser->next = NULL;
    currentUser->vipLevel = 0;
    // 将新用户添加到用户列表中(添加到用户链表头节点)
    if (data->userList == NULL) {
        data->userList = currentUser;
    }
    else {
        currentUser->next = data->userList;
        data->userList = currentUser;
    }
    return OK;
}

/**
 * 用户登录
 *
 * @param data 系统数据结构指针
 * @param phone 号码
 * @param password 密码
 * @return 成功返回用户指针，失败返回NULL
 */
User* loginUser(SystemData* data, const char* phone, const char* password) {
    if (data == NULL || phone == NULL || password == NULL) {
        return NULL;
    }
    // 查找匹配的用户名和密码
    User* current = data->userList;
    while (current != NULL) {
        if (strcmp(current->phone, phone) == 0 && strcmp(current->password, password) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

/**
 * 修改用户信息
 *
 * @param data 系统数据结构指针
 * @param phone 号码
 * @param newPassword 新密码，NULL表示不修改
 * @param newPhone 新手机号，NULL表示不修改
 * @param newAddress 新地址，NULL表示不修改
 * @param currentUser 当前操作用户
 * @return 成功返回OK，失败返回错误码
 */
ErrorCode updateUserInfo(SystemData* data, const char* phone,
    const char* newPassword, const char* newPhone,
    const char* newAddress, User* currentUser) {
    if (data == NULL || phone == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }
    // 只有管理员或用户本人可以修改信息
    if (currentUser->type != USER_ADMIN && strcmp(currentUser->phone, phone) != 0) {
        return ERR_NO_PERMISSION;
    }
    // 查找目标用户，如果未找到目标用户，返回错误
    User* targetUser = data->userList;
    while (targetUser != NULL) {
        if (strcmp(targetUser->phone, phone) == 0) {
            break;
        }
        targetUser = targetUser->next;
    }
    if (targetUser == NULL) {
        return ERR_NOT_FOUND;
    }
    // 修改信息
    if (newPassword != NULL) {
        if (!isPasswordValid(newPassword)) {
            return ERR_INVALID_PARAM;
        }
        strncpy(targetUser->password, newPassword, MAX_PASSWORD_LEN);
    }
    if (newPhone != NULL) {
        if (!isPhoneNumber(newPhone)) {
            return ERR_INVALID_PARAM;
        }
        strncpy(targetUser->phone, newPhone, MAX_PHONE_LEN);
    }
    if (newAddress != NULL) {
        strncpy(targetUser->address, newAddress, MAX_ADDRESS_LEN);
    }
    return OK;
}

/**
 * 删除用户
 *
<<<<<<< HEAD
 * @param data 绯荤粺鏁版嵁缁撴瀯鎸囬拡
 * @param phone 鍙风爜
 * @param currentUser 褰撳墠鎿嶄綔鐢ㄦ埛
 * @return 鎴愬姛杩斿洖OK锛屽け璐ヨ繑鍥為敊璇爜
=======
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param currentUser 当前操作用户
 * @return 成功返回OK，失败返回错误码
>>>>>>> 62002b0 (gb2312)
 */
ErrorCode deleteUser(SystemData* data, const char* phone, User* currentUser) {
    if (data == NULL || phone == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }
    // 只有管理员或用户本人可以注销账户
    if (currentUser->type != USER_ADMIN && strcmp(currentUser->phone, phone) != 0) {
        return ERR_NO_PERMISSION;
    }
    // 查找用户，若未找到返回错误
    User* prev = NULL;
    User* current = data->userList;
    while (current != NULL) {
        if (strcmp(current->phone, phone) == 0) {
            break;
        }
        prev = current;
        current = current->next;
    }
    if (current == NULL) {
        return ERR_NOT_FOUND;
    }
    // 删除目标用户
    if (prev == NULL) {
        data->userList = current->next;
    }
    else {
        prev->next = current->next;
    }
    strcpy(current->phone, "abolished");
    free(current);
    return OK;
}

/**
 * 设置用户VIP
 *
<<<<<<< HEAD
 * @param data 绯荤粺鏁版嵁缁撴瀯鎸囬拡
 * @param phone 鍙风爜
 * @param currentUser 褰撳墠鎿嶄綔鐢ㄦ埛
 * @return 鎴愬姛杩斿洖OK锛屽け璐ヨ繑鍥為敊璇爜
=======
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param currentUser 当前操作用户
 * @return 成功返回OK，失败返回错误码
>>>>>>> 62002b0 (gb2312)
 */
ErrorCode setUserVIPLevel(SystemData* data, const char* phone, User* currentUser) {
    if (data == NULL || phone == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }
    // 只有管理员可以设置VIP
    if (currentUser->type != USER_ADMIN) {
        return ERR_NO_PERMISSION;
    }
    User* targetUser = data->userList;
    while (targetUser != NULL) {
        if (strcmp(targetUser->phone, phone) == 0) {
            break;
        }
        targetUser = targetUser->next;
    }
    if (targetUser == NULL) {
        return ERR_NOT_FOUND;
    }
    targetUser->type = USER_VIP;;
    return OK;
}

/**
 * 根据手机号查找用户
 *
 * @param data 系统数据结构指针
 * @param phone 手机号
 * @return 找到返回用户指针，未找到返回NULL
 */
User* findUserByPhone(SystemData* data, const char* phone) {
    if (data == NULL || phone == NULL) {
        return NULL;
    }
    User* current = data->userList;
    while (current != NULL) {
        if (strcmp(current->phone, phone) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

/**
 * 获取所有用户列表
 *
 * @param data 系统数据结构指针
 * @param userCount 输出参数，返回用户数量
 * @param currentUser 当前操作用户(必须有用户管理权限)
 * @return 用户指针数组，使用完毕需要手动释放，失败返回NULL
 */
User** getAllUsers(SystemData* data, int* userCount, User* currentUser) {
    if (data == NULL || userCount == NULL || currentUser == NULL) {
        return NULL;
    }
    if (currentUser->type != USER_ADMIN) {
        return NULL;
    }
    int count = 0;
    User* current = data->userList;
    while (current != NULL) {
        count++;
        current = current->next;
    }
    User** users = (User**)malloc(count * sizeof(User*));
    if (users == NULL) {
        return NULL;
    }
    current = data->userList;
    for (int i = 0; i < count; i++) {
        users[i] = current;
        current = current->next;
    }
    *userCount = count;
    return users;
}

/**
 * 按用户类型筛选用户
 *
 * @param data 系统数据结构指针
 * @param type 用户类型
 * @param userCount 输出参数，返回用户数量
 * @param currentUser 当前操作用户(必须有用户管理权限)
 * @return 用户指针数组，使用完毕需要手动释放，失败返回NULL
 */
User** getUsersByType(SystemData* data, UserType type, int* userCount, User* currentUser) {
    if (data == NULL || userCount == NULL || currentUser == NULL || type == NULL) {
        return NULL;
    }
    if (currentUser->type != USER_ADMIN) {
        return NULL;
    }
    int count = 0;
    User* current = data->userList;
    while (current != NULL) {
        if (current->type == type) {
            count++;
        }
        current = current->next;
    }
    User** users = (User**)malloc(count * sizeof(User*));
    if (users == NULL) {
        return NULL;
    }
    current = data->userList;
    int index = 0;
    while (current != NULL) {
        if (current->type == type) {
            users[index++] = current;
        }
        current = current->next;
    }
    *userCount = count;
    return users;
}
/**
 * 设置用户权限
 *
<<<<<<< HEAD
 * @param data 绯荤粺鏁版嵁缁撴瀯鎸囬拡
 * @param phone 鍙风爜
 * @param permissions 鏂版潈闄愪綅鎺╃爜
 * @param currentUser 褰撳墠鎿嶄綔鐢ㄦ埛(蹇呴』鏈夌敤鎴风鐞嗘潈闄?
 * @return 鎴愬姛杩斿洖OK锛屽け璐ヨ繑鍥為敊璇爜
=======
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param permissions 新权限位掩码
 * @param currentUser 当前操作用户(必须有用户管理权限)
 * @return 成功返回OK，失败返回错误码
>>>>>>> 62002b0 (gb2312)
 */
ErrorCode setUserPermissions(SystemData* data, const char* phone,
    unsigned int permissions, User* currentUser) {
    if (data == NULL || phone == NULL || permissions == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }
    if (currentUser->type != USER_ADMIN) {
        return ERR_NO_PERMISSION;
    }
    User* targetUser = findUserByPhone(data, phone);
    if (targetUser == NULL) {
        return ERR_NOT_FOUND;
    }
    targetUser->permissions = permissions;
    return OK;
}

/**
 * 添加用户权限(在现有权限基础上增加)
 *
<<<<<<< HEAD
 * @param data 绯荤粺鏁版嵁缁撴瀯鎸囬拡
 * @param phone 鍙风爜
 * @param permission 瑕佹坊鍔犵殑鏉冮檺
 * @param currentUser 褰撳墠鎿嶄綔鐢ㄦ埛(蹇呴』鏈夌敤鎴风鐞嗘潈闄?
 * @return 鎴愬姛杩斿洖OK锛屽け璐ヨ繑鍥為敊璇爜
=======
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param permission 要添加的权限
 * @param currentUser 当前操作用户(必须有用户管理权限)
 * @return 成功返回OK，失败返回错误码
>>>>>>> 62002b0 (gb2312)
 */
ErrorCode addUserPermission(SystemData* data, const char* phone,
    unsigned int permission, User* currentUser) {
    if (data == NULL || phone == NULL || permission == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }
    if (currentUser->type != USER_ADMIN) {
        return ERR_NO_PERMISSION;
    }
    User* targetUser = findUserByPhone(data, phone);
    if (targetUser == NULL) {
        return ERR_NOT_FOUND;
    }
    targetUser->permissions |= permission;
    return OK;
}

/**
 * 移除用户权限
 *
<<<<<<< HEAD
 * @param data 绯荤粺鏁版嵁缁撴瀯鎸囬拡
 * @param phone 鍙风爜
 * @param permission 瑕佺Щ闄ょ殑鏉冮檺
 * @param currentUser 褰撳墠鎿嶄綔鐢ㄦ埛(蹇呴』鏈夌敤鎴风鐞嗘潈闄?
 * @return 鎴愬姛杩斿洖OK锛屽け璐ヨ繑鍥為敊璇爜
=======
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param permission 要移除的权限
 * @param currentUser 当前操作用户(必须有用户管理权限)
 * @return 成功返回OK，失败返回错误码
>>>>>>> 62002b0 (gb2312)
 */
ErrorCode removeUserPermission(SystemData* data, const char* phone,
    unsigned int permission, User* currentUser) {
    if (data == NULL || phone == NULL || permission == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }
    if (currentUser->type != USER_ADMIN) {
        return ERR_NO_PERMISSION;
    }
    User* targetUser = findUserByPhone(data, phone);
    if (targetUser == NULL) {
        return ERR_NOT_FOUND;
    }
    targetUser->permissions &= ~permission;
    return OK;
}

/**
 * 获取用户详细信息的字符串表示
 *
 * @param user 用户指针
 * @param includePassword 是否包含密码信息
 * @param currentUser 当前操作用户
 * @return 包含用户信息的字符串，使用完毕需要手动释放
 */
char* getUserInfoString(User* user, int includePassword, User* currentUser) {
    if (user == NULL || currentUser == NULL) {
        return NULL;
    }
    // 只有用户本人和管理员有权限查看密码
    int canViewPassword = (currentUser->type == USER_ADMIN || currentUser == user);
    // 计算字符串长度
    int length = sprintf(NULL, 0,
        "用户名: %s\n"
        "手机号: %d\n"
        "地址: %s\n"
        "用户类型: %s\n"
        "权限: %s\n"
        "%s",
        user->username,
        user->phone,
        user->address,
        getUserTypeString(user->type),
        getUserPermissionDescription(user->permissions),
        (includePassword && canViewPassword) ? user->password : ""
    );
    char* information = (char*)malloc(length + 1);
    if (information == NULL) {
        return NULL;
    }
    snprintf(information, length + 1,
        "用户名: %s\n"
        "手机号: %d\n"
        "地址: %s\n"
        "用户类型: %s\n"
        "权限: %s\n"
        "%s",
        user->username,
        user->phone,
        user->address,
        getUserTypeString(user->type),
        getUserPermissionDescription(user->permissions),
        (includePassword && canViewPassword) ? user->password : ""
    );
    return information;
}

/**
 * 显示用户管理菜单并处理用户选择
 *
 * @param data 系统数据结构指针
 * @param currentUser 当前登录用户(必须是管理员)
 */
void showUserManagementMenu(SystemData* data, User* currentUser) {
    if (currentUser->type != USER_ADMIN) {
        printf("您没有权限访问用户管理功能!\n");
        printf("\n按Enter键继续...");
        getchar();
        return;
    }

    int running = 1;
    while (running) {
        system("cls");  // 清屏

        printf("\n=== 用户管理 ===\n");
        printf("1. 查看所有用户\n");
        printf("2. 添加新用户\n");
        printf("3. 修改用户信息\n");
        printf("4. 删除用户\n");
        printf("5. 设置用户权限\n");
        printf("6. 设置用户VIP等级\n");
        printf("7. 按用户类型查看\n");
        printf("0. 返回主菜单\n");
        printf("\n请选择: ");

        int choice;
        scanf("%d", &choice);
        getchar();  // 吸收换行符

        switch (choice) {
        case 0:  // 返回主菜单
            running = 0;
            break;

        case 1: {  // 查看所有用户
            int userCount = 0;
            User** users = getAllUsers(data, &userCount, currentUser);

            if (users == NULL || userCount == 0) {
                printf("\n没有找到用户!\n");
            }
            else {
                printf("\n系统共有 %d 个用户:\n", userCount);
                printf("-----------------------------------------------\n");
                printf("%-15s %-10s %-15s %-15s\n", "用户名", "类型", "手机号", "权限");
                printf("-----------------------------------------------\n");

                for (int i = 0; i < userCount; i++) {
                    char* permDesc = getUserPermissionDescription(users[i]->permissions);
                    printf("%-15s %-10s %-15s %-15s\n",
                        users[i]->username,
                        getUserTypeString(users[i]->type),
                        users[i]->phone,
                        permDesc);
                    free(permDesc);
                }

                free(users);  // 释放用户指针数组
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        case 2: {  // 添加新用户
            char username[MAX_USERNAME_LEN];
            char password[MAX_PASSWORD_LEN];
            char phone[MAX_PHONE_LEN];
            char address[MAX_ADDRESS_LEN];
            UserType type;

            printf("\n=== 添加新用户 ===\n");
            printf("用户名: ");
            scanf("%s", username);
            getchar();

            printf("密码: ");
            scanf("%s", password);
            getchar();

            printf("手机号: ");
            scanf("%s", phone);
            getchar();

            printf("地址: ");
            scanf("%s", address);
            getchar();

            printf("用户类型(1:学生, 2:教师, 3:快递员, 4:工作人员, 5:管理员): ");
            scanf("%d", (int*)&type);
            getchar();

            ErrorCode result = registerUser(data, username, password, phone, address, type, currentUser);

            if (result == OK) {
                printf("\n用户添加成功!\n");
            }
            else {
                printf("\n用户添加失败! 错误码: %d\n", result);
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        case 3: {  // 修改用户信息
            char phone[MAX_PHONE_LEN];
            char newPassword[MAX_PASSWORD_LEN];
            char newPhone[MAX_PHONE_LEN];
            char newAddress[MAX_ADDRESS_LEN];

            printf("\n=== 修改用户信息 ===\n");
            printf("请输入要修改的用户手机号: ");
            scanf("%s", phone);
            getchar();

            User* user = findUserByPhone(data, phone);
            if (user == NULL) {
                printf("\n未找到该用户!\n");
                printf("\n按Enter键继续...");
                getchar();
                break;
            }

            char* userInfo = getUserInfoString(user, 0, currentUser);
            printf("\n当前用户信息:\n%s\n", userInfo);
            free(userInfo);

            printf("\n新密码(留空表示不修改): ");
            fgets(newPassword, sizeof(newPassword), stdin);
            newPassword[strcspn(newPassword, "\n")] = 0;

            printf("新手机号(留空表示不修改): ");
            fgets(newPhone, sizeof(newPhone), stdin);
            newPhone[strcspn(newPhone, "\n")] = 0;

            printf("新地址(留空表示不修改): ");
            fgets(newAddress, sizeof(newAddress), stdin);
            newAddress[strcspn(newAddress, "\n")] = 0;

            ErrorCode result = updateUserInfo(data, phone,
                strlen(newPassword) > 0 ? newPassword : NULL,
                strlen(newPhone) > 0 ? newPhone : NULL,
                strlen(newAddress) > 0 ? newAddress : NULL,
                currentUser);

            if (result == OK) {
                printf("\n用户信息修改成功!\n");
            }
            else {
                printf("\n用户信息修改失败! 错误码: %d\n", result);
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        case 4: {  // 删除用户
            char phone[MAX_PHONE_LEN];

            printf("\n=== 删除用户 ===\n");
            printf("请输入要删除的用户手机号: ");
            scanf("%s", phone);
            getchar();

            // 确认删除
            printf("\n确定要删除该用户吗? 此操作不可恢复 (y/n): ");
            char confirm;
            scanf("%c", &confirm);
            getchar();

            if (confirm == 'y' || confirm == 'Y') {
                ErrorCode result = deleteUser(data, phone, currentUser);

                if (result == OK) {
                    printf("\n用户删除成功!\n");
                }
                else {
                    printf("\n用户删除失败! 错误码: %d\n", result);
                }
            }
            else {
                printf("\n已取消删除操作!\n");
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        case 5: {  // 设置用户权限
            char phone[MAX_PHONE_LEN];
            int operation;
            unsigned int permission;

            printf("\n=== 设置用户权限 ===\n");
            printf("请输入用户手机号: ");
            scanf("%s", phone);
            getchar();

            User* user = findUserByPhone(data, phone);
            if (user == NULL) {
                printf("\n未找到该用户!\n");
                printf("\n按Enter键继续...");
                getchar();
                break;
            }

            char* permDesc = getUserPermissionDescription(user->permissions);
            printf("\n当前用户权限: %s\n", permDesc);
            free(permDesc);

            printf("\n操作类型:\n");
            printf("1. 添加权限\n");
            printf("2. 移除权限\n");
            printf("3. 设置全新权限\n");
            printf("请选择: ");
            scanf("%d", &operation);
            getchar();

            printf("\n可用权限(位掩码):\n");
            printf("1 - 包裹入库\n");
            printf("2 - 包裹出库\n");
            printf("4 - 用户管理\n");
            printf("8 - 货架管理\n");
            printf("16 - 数据统计\n");
            printf("32 - 系统设置\n");

            ErrorCode result = ERR_SYSTEM;

            switch (operation) {
            case 1:  // 添加权限
                printf("\n请输入要添加的权限(数字): ");
                scanf("%u", &permission);
                getchar();
                result = addUserPermission(data, phone, permission, currentUser);
                break;

            case 2:  // 移除权限
                printf("\n请输入要移除的权限(数字): ");
                scanf("%u", &permission);
                getchar();
                result = removeUserPermission(data, phone, permission, currentUser);
                break;

            case 3:  // 设置全新权限
                printf("\n请输入新的权限掩码(数字): ");
                scanf("%u", &permission);
                getchar();
                result = setUserPermissions(data, phone, permission, currentUser);
                break;
            }

            if (result == OK) {
                printf("\n权限设置成功!\n");
            }
            else {
                printf("\n权限设置失败! 错误码: %d\n", result);
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        case 6: {  // 设置用户VIP等级
            char phone[MAX_PHONE_LEN];

            printf("\n=== 设置用户VIP等级 ===\n");
            printf("请输入用户手机号: ");
            scanf("%s", phone);
            getchar();

            User* user = findUserByPhone(data, phone);
            if (user == NULL) {
                printf("\n未找到该用户!\n");
            }
            else if (user->type != USER_STUDENT) {
                printf("\n只有学生和教师用户可以设置VIP等级!\n");
            }
            else {
                ErrorCode result = setUserVIPLevel(data, phone, currentUser);

                if (result == OK) {
                    printf("\nVIP等级设置成功!\n");
                }
                else {
                    printf("\nVIP等级设置失败! 错误码: %d\n", result);
                }
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        case 7: {  // 按用户类型查看
            UserType type;

            printf("\n=== 按用户类型查看 ===\n");
            printf("请选择用户类型:\n");
            printf("1. 学生\n");
            printf("2. 教师\n");
            printf("3. 快递员\n");
            printf("4. 工作人员\n");
            printf("5. 管理员\n");
            printf("请输入: ");
            scanf("%d", (int*)&type);
            getchar();

            int userCount = 0;
            User** users = getUsersByType(data, type, &userCount, currentUser);

            if (users == NULL || userCount == 0) {
                printf("\n没有找到该类型的用户!\n");
            }
            else {
                printf("\n找到 %d 个 %s 用户:\n", userCount, getUserTypeString(type));
                printf("-----------------------------------------------\n");
                printf("%-15s %-15s %-20s\n", "用户名", "手机号", "地址");
                printf("-----------------------------------------------\n");

                for (int i = 0; i < userCount; i++) {
                    printf("%-15s %-15s %-20s\n",
                        users[i]->username,
                        users[i]->phone,
                        users[i]->address);
                }

                free(users);  // 释放用户指针数组
            }

            printf("\n按Enter键继续...");
            getchar();
            break;
        }

        default:
            printf("\n无效选项!\n");
            printf("\n按Enter键继续...");
            getchar();
            break;
        }
    }
}

/**
 * 显示我的通知菜单
 *
 * @param data 系统数据结构指针
 * @param currentUser 当前登录用户
 */
void showMyNotificationsMenu(SystemData* data, User* currentUser);

/**
 * 获取用户权限描述
 *
 * @param permissions 权限位掩码
 * @return 权限的文字描述，使用完毕需要手动释放
 */
char* getUserPermissionDescription(unsigned int permissions) {
    const char* permissionNames[] = {
        "用户管理权限", "包裹入库权限", "包裹出库权限", "包裹查询权限",
        "货架管理权限", "系统管理权限", "查看统计数据权限", "查看日志权限"
    };
    const unsigned int permissionMasks[] = {
        PERM_USER_MGMT, PERM_PACKAGE_ADD, PERM_PACKAGE_PICKUP, PERM_PACKAGE_QUERY,
        PERM_SHELF_MGMT, PERM_SYSTEM_MGMT, PERM_STATS_VIEW, PERM_LOG_VIEW
    };
    // 检查是否包含所有权限
    unsigned int allPermissions = 0;
    for (int i = 0; i < sizeof(permissionMasks) / sizeof(permissionMasks[0]); i++) {
        allPermissions |= permissionMasks[i];
    }

    if ((permissions & allPermissions) == allPermissions) {
        char* description = (char*)malloc(13);
        if (description == NULL) {
            return NULL;
        }
        strcpy(description, "所有权限");
        return description;
    }
    int length = 0;
    for (int i = 0; i < sizeof(permissionMasks) / sizeof(permissionMasks[0]); i++) {
        if (permissions & permissionMasks[i]) {
            length += strlen(permissionNames[i]) + 2;
        }
    }
    //检查是否没有任何权限
    if (length == 0) {
        char* description = (char*)malloc(10);
        if (description == NULL) {
            return NULL;
        }
        strcpy(description, "无权限");
        return description;
    }
    //输出部分权限
    char* description = (char*)malloc(length + 1);
    if (description == NULL) {
        return NULL;
    }
    description[0] = '\0';
    for (int i = 0; i < sizeof(permissionMasks) / sizeof(permissionMasks[0]); i++) {
        if (permissions & permissionMasks[i]) {
            if (description[0] != '\0') {
                strcat(description, ", ");
            }
            strcat(description, permissionNames[i]);
        }
    }
    return description;
}

/**
 * 获取用户类型的字符串描述
 *
 * @param type 用户类型
 * @return 类型字符串描述
 */
const char* getUserTypeString(UserType type) {
    switch (type) {
    case USER_STUDENT: return "普通用户";
    case USER_VIP: return "VIP用户";
    case USER_COURIER: return "快递员";
    case USER_STAFF: return "驿站工作人员";
    case USER_ADMIN: return "系统管理员";
    default: return "未知";
    }
}

#endif // USER_MANAGEMENT_C